// 工程化-模块化
import Vue from 'vue'
import VueRouter from 'vue-router'
// 明确的安装路由功能
Vue.use(VueRouter)

import {
    Toast
} from 'vant'
// 创建路由对象添加路由配置
const router = new VueRouter({
    // 添加配置
    routes: [{
            path: '/',
            redirect: {
                name: 'index'
            }
        },
        {
            name: 'index',
            path: '/index',
            component: () => import('@/views/index.vue')
        },
        {
            name: 'login',
            path: '/login',
            component: () => import('@/views/user/login.vue')
        },
        {
            name: 'register',
            path: '/register',
            component: () => import('@/views/user/register.vue')
        },
        {
            name: 'personal',
            path: '/personal/:id',
            component: () => import('@/views/user/personal.vue')
        },
        {
            name: 'edit_profile',
            path: '/edit_profile/:id',
            component: () => import('@/views/user/edit_profile.vue')
        },
        {
            name: 'postDetail',
            path: '/postDetail/:id',
            component: () => import('@/views/post/postDetail.vue')
        },
        {
            name: 'myfollow',
            path: '/myfollow',
            component: () => import('@/views/user/myfollow.vue')
        },
        {
            name: 'mystar',
            path: '/mystar',
            component: () => import('@/views/user/mystar.vue')
        },
        {
            name: 'comment',
            path: '/comment/:id',
            component: () => import('@/views/comment.vue')
        },
        {
            name: 'cateManager',
            path: '/cateManager',
            component: () => import('@/views/cateManager.vue')
        },
        {
            name: 'search',
            path: '/search',
            component: () => import('@/views/search.vue')
        },
    ]
})
// 导航守卫
router.beforeEach((to, from, next) => {
    let arr = ['personal', 'eait_profile']
    // if (to.path.indexOf('/personal/') !== -1) {
    if (arr.indexOf(to.name) !== -1) {
        // 验证token
        let token = localStorage.getItem('heimatoutiao_token')
        if (token) {
            next()
        } else {
            // 提示
            Toast('未登录，请先登录')
            // 重定向到登录页
            next({
                name: 'login'
            })
        }
    } else {
        next()
    }

})
// 暴露
export default router